package com.china_goldcard.igas.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.support.annotation.Nullable;

/**
 * Created by jk on 16/10/26.
 */

public class MessengerService extends Service {

    private static final int MSG_SUM = 0x110;

    private static Messenger mMessenger = new Messenger(new Handler(){

        @Override
        public void handleMessage(Message msgfromClient) {
            Message msgToClient = Message.obtain(msgfromClient);
            switch (msgToClient.what){
                case MSG_SUM:
                    try{
                        //模拟耗时
                        Thread.sleep(2000);
                        msgToClient.arg2 = msgfromClient.arg1 + msgfromClient.arg2;
                        msgfromClient.replyTo.send(msgToClient);
                    }catch (Exception ex) {

                    }

            }
            super.handleMessage(msgfromClient);
        }
    });

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
}
